class Solution(object):
    def minimumCost(self, m, n, horizontalCut, verticalCut):
        """
        :type m: int
        :type n: int
        :type horizontalCut: List[int]
        :type verticalCut: List[int]
        :rtype: int
        """
        cuts = [(c, 0) for c in horizontalCut] + [(c, 1) for c in verticalCut]
        cuts.sort(reverse=True)

        h_pieces = 1
        v_pieces = 1
        total_cost = 0

        for cost, t in cuts:
            if t == 0:
                total_cost += cost * v_pieces
                h_pieces += 1
            else:
                total_cost += cost * h_pieces
                v_pieces += 1

        return total_cost
